<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Config;
use think\Request;
use think\helper\Time;
use think\Loader;
use think\Image;

use app\admin\model\ComValidate;
use app\admin\model\ComModel;

class Common extends Controller{

	public $dir="./app/index/view/index/";

	public function __construct(){

		parent::__construct(); 
		
		/*return true;
*/
		//return true;
		if(!session('admin')){
			$this->redirect('login/index');
			//alert("请先登录！",Url('Admin/login'));
		}

		//超级管理员 越过权限验证~
		if(session('admin.adminid')=='1'){
			return true;
		}

		$roleid=ComModel::data_value('admin_list',['adminid'=>session('admin.adminid')],"roleid");
		$roleinfo=ComModel::data_find('admin_role',['roleid'=>$roleid]);
		if($roleinfo==''){
			session('admin',null);
			$this->error("当前管理角色不存在，请联系管理员~",Url('Admin/login'));
		}
		if($roleinfo['status']=='0'){
			session('admin',null);
			$this->error("当前管理角色已被禁用，请联系管理员~",Url('Admin/login'));
		} 

		//当前角色所有权限
		$auto=explode(",",$roleinfo['auto']);

		$request = Request::instance();

		$controller=$request->controller();
		$action=$request->action();

		$map=array(
			'value'=>$controller,
			'type'=>"控制器",
		);
		$controllerinfo=ComModel::data_find('admin_action',$map);
		if(empty($controllerinfo)){
			$this->error("当前控制器未录入，您暂无权限查看！");
		}else if($controllerinfo['status'] !="1"){
			$this->error("当前控制器已被禁用！！");
		}

		$map=array(
			'value'=>$controller."/".$action,
			'type'=>"操作",
		);
		$actioninfo=ComModel::data_find('admin_action',$map);
		if(empty($actioninfo)){
			$this->error("当前操作未录入，您暂无权限查看！");
		}else if($actioninfo['status'] !='1'){
			$this->error("当前操作已被禁用！！");
		}else if(!in_array($actioninfo['actionid'],$auto)){
			$this->error("您暂无权限！");
		}





	}
	
	#控制器初始化#
	/*public function _initialize(){
		
		//return true;
		if(!session('admin')){
			$this->redirect('login/index');
			//alert("请先登录！",Url('Admin/login'));
		}
		$sid=session('sid');

		$rule_mode=config('rule_mode');

		if($rule_mode=='2'){ //严格模式，每一步都要判断
			$groupid=Db::name('admin_list')->where('id',$sid)->value('groupid');
			$role=Db::name('admin_role')->where('id',$groupid)->find();
			if($role['status']!='1'){
				session('sid',null);
				alert("当前状态已被禁用，请重试！",Url('Admin/login'));
			}else{
				if($role['rule']!='alladmin'){ //非超级管理员
					$rulegroup=explode(",",$role['rule']);

					$request = Request::instance();

					$controller=$request->controller();
					$action=$request->action();
					$action=array(
						'controller'=>strtolower($controller),
						'action'=>strtolower($action),
						'status'=>"1"
					);

					$ruleid=Db::name('admin_rule_list')->where($map)->value('id');
					if(!$ruleid){ //不在权限里面，404
						//404
						exit;
					}
					if(!in_array($ruleid,$rulegroup)){
						alert("暂无权限！");
					}
				}
				
			}

		}else if($rule_mode=='1'){ //非严格模式,判断是否在session里面
			if(!session('rule')){
				$groupid=Db::name('admin_list')->where('id',$sid)->value('groupid');
				$role=Db::name('admin_role')->where('id',$groupid)->find();
				$rule=explode(",",$role['rule']);
				session('rule',$rule);
			}

			$request = Request::instance();

			$controller=$request->controller();
			$action=$request->action();

			$action=array(
				'controller'=>strtolower($controller),
				'action'=>strtolower($action),
				'status'=>"1"
			);

			$ruleid=Db::name('admin_rule_list')->where($map)->value('id');
			if(!$ruleid){ //不在权限里面，404
				//404
				exit;
			}
			if(!in_array($ruleid,session('rule'))){
				alert("暂无权限！");
			}

		}
		
    }*/
    public function index(){
    	echo "";
    }
	#上传#
	public function fileupload(){
		if(request()->ispost()){
		
			$file = request()->file('file');


			$type=input('type');

			$catalog=input("catalog","file");

			//文件上传路径
			//$file_up_path=ROOT_PATH . 'public' . DS . 'uploads'. DS .$catalog;
			$file_up_path=ROOT_PATH . '/public/uploads/file';

			//创建目录
			if(!is_dir($file_up_path)){
				mkdir ($file_up_path,0777,true);	
			}

			$width=input('width');
			$height=input('height');
			$other=input('other');

			$info = $file->move($file_up_path);
		    if($info){

		    	// 成功上传后 获取上传信息
		        $path=$file_up_path. "/" .$info->getSaveName();

		        $img_type=['jpeg','jpg','png','gif'];
		        //如果是图片
		    	if(in_array($info->getExtension(),$img_type)){
			       //缩略图
			       if($width || $height){
			       		$image = \think\Image::open($path);
			       		$image->thumb($width, $height,\think\Image::THUMB_CENTER)->save($path);
			       }

				   $config=config('site');

				   //水印
				   /*if($other=='water'){
				   		//path'=>DS .'public' . DS . 'uploads'. DS .$catalog. DS .$info->getSaveName(),
					   	$arr=array(
					   		'path'=>'/public/uploads/file/'.str_replace('\\','/',$info->getSaveName()),
					   		'status'=>'1',
					   		'filename'=>$info->getInfo()['name'] //文件原来的名称
					   	);
				       return json($arr);
				   }*/
				   if($other=='newinfo'){
					   if($config['water']=='img'){
					   		$image = \think\Image::open($path);
							$image->water(ROOT_PATH.$config['wate_path'],$config['wate_position'],$config['wate_transparent'])->save($path);
					   }else if($config['water']=='text'){
					   		$image = \think\Image::open($path);
					   		$font=ROOT_PATH."/public/font/".$config['text_font'];
							$image->text($config['wate_text'],$font,$config['text_size'],$config['text_color'])->save($path);
					   }
					}
				}
			    
			    $arr=array(
			   		'path'=>'/public/uploads/file/'.str_replace('\\','/',$info->getSaveName()),
			   		'status'=>'1',
			   		'filename'=>$info->getInfo()['name'] //文件原来的名称
			   	);
		       return json($arr);
		    }else{
		    	return json($file->getError());
		    }


	    }else{
	    	$this->error("当前页面不存在~");
	    }
	
	}

	#删除文件#
	public function removefile($file=""){
		//if(request()->isAjax()){
			$file=!empty($file)?$file:input("file");
			if(is_file(".".$file)){
				unlink(".".$file);
				//$this->success("文件删除成功！");
			}
			//$this->error($file."文件删除失败！");
		//}
	}
	#管理员操作记录#
	protected function log($text){

		$data=array(
			'adminid'=>session('admin.adminid'),
			'name'=>session('admin.nickname'),
			'content'=>"管理员:".session('admin.nickname')."__".$text,
			'addtime'=>date('Y-m-d H:i:s',time())
		);
		
		Db::name('admin_log')->insert($data);

	}
	
	#阿里大鱼短信验证码# //传入手机号
	protected function yzm($tel){
        //阿里大鱼
        //$tel=input('post.tel');
        $num=rand(10000,99999);
        session('yzm',$num);
        session('tel',$tel);
       
        Loader::import('ali.TopSdk', EXTEND_PATH);
        $c = new \TopClient;
        $c->appkey = "";
        $c->secretKey = "";
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend("123456");
        $req->setSmsType("normal");
        $req->setSmsFreeSignName("网站");
        $req->setSmsParam("{\"number\":\"".$num."\"}");
        $req->setRecNum($tel);
        $req->setSmsTemplateCode("SMS_56230225");
        $resp = $c->execute($req);
       
    }

    #获取所有页面模板#
    protected function gettemplate(){
    	
    	//目录下文件
		$template=array("0"=>array(),"1"=>array(),"2"=>array() );

	    foreach (scandir($this->dir) as $k => $v) {
	    	//单页
	       if(preg_match("/page_(.*?).html/",$v,$r)==true){

	            array_push($template[0],str_replace(".html","",$v));
	        //列表页
	       }else if(preg_match("/list_(.*?).html/",$v,$r)==true){
	       		array_push($template[1],str_replace(".html","",$v));
	        //内容页
	       }else if(preg_match("/content_(.*?).html/",$v,$r)==true){
	       		array_push($template[2],str_replace(".html","",$v));
	       }

	    }

	    return $template;
    }

    #所有权限操作#
	protected function actiondatalist(){
		$list=Db::name("admin_action")->where(['type'=>"控制器",'status'=>"1"])->order('px desc')->select();
		foreach ($list as $k => $v) {
			$list[$k]['list']=Db::name("admin_action")->where(['type'=>"操作",'status'=>"1",'pid'=>$v['actionid']])->order('px desc')->select();
		}

		return $list;
	}
    
	
	




}
